package hypeerweb.visitor;

import hypeerweb.node.Node;

public abstract class SendVisitor implements Visitor {

	protected static final String TARGET_KEY = "target key";

	public SendVisitor() {

	}

	protected abstract void targetOperation(Node node, Parameters parameters);

	@Override
	public void visit(Node node, Parameters parameters) {
		int target = (Integer) parameters.get(TARGET_KEY);
		if (node.getWebId().getValue() == target) {
			targetOperation(node, parameters);
		} else {
			intermediateOperation(node, parameters);
			Node nextNode = node.getClosestNodeTo(target);
			nextNode.accept(this, parameters);
		}
	}

	public static Parameters createInitialParameters(int target) {
		Parameters params = new Parameters();
		params.set(TARGET_KEY, target);
		return params;
	}

	protected void intermediateOperation(Node node, Parameters parameters) {
		if (parameters.containsKey("trace intermediates")) {
			System.out.println("Intermediate: " + node.getWebId() + " ("
					+ node.getWebId().getValue() + ")");
		}
	}
}
